public class Solution4 {
    public static void main(String[] args) {
        int n = 4;
        System.out.println(countAndSay(n));
    }
    public static String countAndSay(int n) {
        String ret = "1";
        for(int i = 1; i < n; i++) {
            char[] ch = ret.toCharArray();
            ret = "";
            int left = 0;
            int right = 0;
            while(right < ch.length) {
                if(ch[left] == ch[right]) {
                    right++;
                }else {
                    ret += right - left;
                    ret += ch[left];
                    left = right;
                }
            }
            ret += right - left;
            ret += ch[left];
        }
        return ret;
    }
    public static String countAndSay2(int n) {
        String ret = "1";
        for(int i = 1; i < n; i++) { // 解释 n - 1 次 ret
            StringBuilder temp = new StringBuilder();
            int len = ret.length();
            for(int left = 0, right = 0; right < len; ) {
                while(right < len && ret.charAt(left) == ret.charAt(right)) {
                    right ++;
                }
                temp.append(Integer.toString(right - left));
                temp.append(ret.charAt(left));
                left = right;
            }
            ret = temp.toString();
        }
        return ret;
    }
}
